/*
Javascript Rich Text Editor v0.1
Copyright (C) 2007  Matt Makins (mattmakins@gmail.com)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

var iViewDoc, iViewListener;

function Init(){

  setInnerHTML();

  if(navigator.appName == "Microsoft Internet Explorer"){
    iViewDoc = iView.document;
    iViewListener = iView;
  }
  else{
    iViewDoc = document.getElementById('iView').contentDocument;
    iViewListener = document.getElementById('iView').contentWindow;
  }

  iViewDoc.designMode = "on";

  if(navigator.appName == "Microsoft Internet Explorer"){
    iViewDoc.attachEvent("onkeyup", updateButtons);
    iViewDoc.attachEvent("onkeydown", updateButtons);
    iViewDoc.attachEvent("onclick", updateButtons);
  }
  else{
    iViewListener.document.addEventListener("keyup", updateButtons, true);
    iViewListener.document.addEventListener("keydown", updateButtons, true);
    iViewListener.document.addEventListener("click", updateButtons, true);
  }

  left();  // default aliginment is left
}

function getInnerHTML(){

  if(navigator.appName == "Microsoft Internet Explorer"){
    return document.frames['iView'].document.body.innerHTML;
  }
  else{
    return document.getElementById('iView').contentDocument.body.innerHTML;
  }
}

function setInnerHTML(){

  if(navigator.appName == "Microsoft Internet Explorer"){
    document.frames['iView'].document.body.innerHTML =   document.getElementById('entry').value;
  }
  else{
    document.getElementById('iView').contentDocument.body.innerHTML =   document.getElementById('entry').value;
  }
}

function left(){

  iViewDoc.execCommand('justifyleft', false, null);
  buttClick('justifyleftTd', 'justifyleft');

  if (iViewDoc.queryCommandState('justifyleft')){
    buttClick('justifycenterTd', 'justifycenter');
    buttClick('justifyrightTd', 'justifyright');
  }
}

function right(){

  iViewDoc.execCommand('justifyright', false, null);
  buttClick('justifyrightTd','justifyright');

  if (iViewDoc.queryCommandState('justifyright')){
    buttClick('justifycenterTd', 'justifycenter');
    buttClick('justifyleftTd', 'justifyleft');
  }
}

function center(){

  iViewDoc.execCommand('justifycenter', false, null);
  buttClick('justifycenterTd','justifycenter');

  if (iViewDoc.queryCommandState('justifycenter')){
    buttClick('justifyleftTd','justifyleft');
    buttClick('justifyrightTd', 'justifyright');
  }
}

function bold(){

  iViewDoc.execCommand('bold', false, null);
  buttClick('boldTd', 'bold');
}

function buttClick(button, style){

  if (iViewDoc.queryCommandState(style)){
    document.getElementById(button).style.borderColor = '#000000';
    document.getElementById(button).style.backgroundColor = '#CCCCCC';
  } 
  else{
    document.getElementById(button).style.backgroundColor = '#EEEEEE';
  }
  iViewListener.focus();
}

function italic(){

  iViewDoc.execCommand('italic', false, null);
  buttClick('italicTd', 'italic');
}

function underline(){

  iViewDoc.execCommand('underline', false, null);
  buttClick('underlineTd','underline');
}

function bullets(){

  iViewDoc.execCommand('insertunorderedlist',false,null);
  buttClick('insertunorderedlistTd','insertunorderedlist');
}

function numbers(){

  iViewDoc.execCommand('insertorderedlist',false,null);
  buttClick('insertorderedlistTd','insertorderedlist');
}

function hyperlink(){

  var szURL = prompt("Enter the URL", "");
  iViewDoc.execCommand('createlink',false,szURL);
}

function image(){

	document.getElementById("image-select").style.visibility="visible";
/*
  var imgSrc = prompt('Enter image location', '');
  if (imgSrc != null){
    document.getElementById('iView').contentDocument.execCommand('insertimage',false,imgSrc);
  }
*/
}

function close(){
	document.getElementById("image-select").style.visibility="hidden";
}

function updateButtons(){

  var buttons = new Array();

  buttons[0] = 'bold';
  buttons[1] = 'italic';
  buttons[2] = 'underline';
  buttons[3] = 'justifyleft';
  buttons[4] = 'justifycenter';
  buttons[5] = 'justifyright';
  buttons[6] = 'insertunorderedlist';
  buttons[7] = 'insertorderedlist';

  for (i=0;i<buttons.length;i++){

    buttClick(buttons[i]+'Td',buttons[i]);
  }
}

function submitPost(){

  document.getElementById('entry').value = getInnerHTML();
}
